import {action, observable} from "mobx";
import {ListViewPage, SearchAdvanced} from "weaver-mobile-page";
import {Bubble, Tools} from "weaver-mobile";
import * as API from "../../api";

const getLabel = Tools.getLabel
export default class TaskStore {
    @observable searchValue = "";   //search
    @observable show1 = false;
    @observable show2 = false;
    @observable val1 = 2;//默认值为全部
    @observable val2 = 0;
    @observable tabkey = 0;
    @observable conditionReady = false;
    @observable listAd = new ListViewPage.Store({route: "/index/task", id: "0"});
    @observable searchAd = new SearchAdvanced.Store({route: "/index/task"});
    @observable taskOpt = {
        0: "execute",
        1: "approval",
        2: "search",
    };
    @observable conditionFetchOptions = {
        url: "/api/prj/moblie/task/getTaskSearchCondition",
        method: "GET",
        params: {"conditiontype": this.taskOpt[this.val1]}
    };

    @action
    init = (taskState, progress, fresh) => {
        if (taskState) {
            this.show1 = false;
            this.val1 = Number(taskState);
            this.conditionFetchOptions = {
                url: "/api/prj/moblie/task/getTaskSearchCondition",
                method: "GET",
                params: {"conditiontype": this.taskOpt[this.val1]}
            };
        }
        if (progress) {
            this.show2 = false;
            this.val2 = Number(progress);
            this.tabkey = this.val2;
        }
        this.initSearchAd(fresh, this.onSearch);
    };

    @action
    initSearchAd = (fresh, callback) => {
        if (fresh === undefined || fresh === "yes") {
            this.searchAd.form.resetFields()
            this.searchAd.getData(this.conditionFetchOptions).then((form) => {
                if (this.searchValue) {
                    form.updateField("taskname", this.searchValue);
                }
                callback();
                this.judgeIconShow();
            });
        }else{
            this.onSearch()
            this.judgeIconShow();
        }
    }
    @action
    changeSearchValue = (val = "") => {
        this.searchValue = val;
        this.searchAd.form.updateField("taskname", val);
        this.judgeIconShow();
    };
    @action
    onSearchAdvancedChange = (data, value) => {
        const {formParams} = this.searchAd.form
        this.searchValue = formParams["taskname"]
        this.judgeIconShow();
    }
    @action
    judgeIconShow = () => {
        const {formParams} = this.searchAd.form
        this.conditionReady = false;
        Object.keys(formParams).forEach(v => {
            if (formParams[v]) {
                this.conditionReady = true;
            }
        })
    }
    @action
    onReset = () => {
        this.changeSearchValue();
    }

    @action
    onSearch = () => {
        let list, params;
        list = this.listAd;
        params = {"conditiontype": this.taskOpt[this.val1], "taskname": this.searchValue};
        if (this.val2 !== 0) {
            params = {...params, tabkey: this.tabkey}
        }
        Object.assign(params, this.searchAd.form.formParams);
        // 清除列表数据
        list.clearData();
        // 带参数请求列表数据
        this.getTaskList(list, params)
    };
    @action
    getTaskList = (list, params) => {
        API.getTaskList(params).then(action(res => {
            list.getData({dataKey: res.datas});
        }))
    };

    @action
    changeShow1 = () => {
        this.show1 = !this.show1;
        if (this.show1) {
            this.show2 = false;
        }
    }
    @action
    changeShow2 = () => {
        this.show2 = !this.show2;
        if (this.show2) {
            this.show1 = false;
        }
    }
    @action
    chooseVal1 = (v, history) => {
        this.show1 = false;
        this.val1 = v.pop();
        this.conditionFetchOptions = {
            url: "/api/prj/moblie/task/getTaskSearchCondition",
            method: "GET",
            params: {"conditiontype": this.taskOpt[this.val1]}
        };
        history.push(`/index/task?taskState=${this.val1}&progress=${this.val2}`)
        this.initSearchAd("yes", this.onSearch)

    };
    @action
    chooseVal2 = (v, history) => {
        this.show2 = false;
        this.tabkey = this.val2 = v.pop();
        history.push(`/index/task?taskState=${this.val1}&progress=${this.val2}`)
        this.onSearch();
    }

    @action
    approve = (id) => {
        const params = {
            method: "approve",
            taskids: id
        }
        API.doTaskApproval(params).then(action(res => {
            if (res.success) {
                Bubble.success(getLabel("1009", "审批通过"), 1)
                this.init()
            } else {
                Bubble.error(res.msg, 1);
            }
        }))
    }
    @action
    refuse = (id) => {
        const params = {
            method: "refuse",
            taskids: id
        }
        API.doTaskApproval(params).then(action(res => {
            if (res.success) {
                Bubble.success(getLabel("500830", "任务已退回"), 1)
                this.init(this.val1, this.val2)
            } else {
                Bubble.error(res.msg, 1);
            }
        }))
    }
}
